﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quexcel.OpenXML.Interfaces;
using Quexcel.OpenXML.Excel.Style;
using Quexcel.OpenXML.Exceptions;
using Quexcel.OpenXML.Excel.SharedString;

namespace Quexcel.OpenXML.Excel.Worksheet.Components
{
    public class Cell : ICell
    {
        private Address cellAddress;
        private DataType cellType;
        private int styleIndex;
        private CellStyle style;
        private Row row;
        private object dataValue;

        private Cell(Address cellAddress, int styleIndex, Row row)
        {
            this.cellAddress = cellAddress;
            this.styleIndex = styleIndex;
            this.row = row;
        }

        public static Cell CreateCell(Address cellAddress, int styleIndex, Row row)
        {
            return new Cell(cellAddress, styleIndex, row);
        }

        #region ICell Members

        public Address CellAddress
        {
            get { return cellAddress; }
        }

        public object Value
        {
            get
            {
                return dataValue; // worksheet.GetReader().Read(cellAddress.ToString());
            }set
            {
                try
                {
                    string finalData = "";
                    int sharedStringIndex = -1;

                    #region Validate the data to be inserted
                    switch (Type)
                    {
                        case DataType.Char:
                            if (value == null
                                || value.ToString().Length <= 1)
                            {
                                finalData = value == null ? "" : value.ToString();
                                /*if (finalData != "")
                                {
                                    SharedStringData sharedString = worksheet.GetSharedString();
                                    if ((sharedStringIndex = sharedString.GetReader().CheckSharedString(finalData)) == -1)
                                        sharedStringIndex = sharedString.GetWriter().WriteSharedString(finalData);
                                }*/
                            }
                            else
                                throw new InvalidDataException(value
                                    , String.Format("The value '{0}' provided for the '{1}' ColumnType is not valid", value, DataType.Char.ToString()));
                            break;
                        case DataType.Datetime:
                            if (value == null
                                || DateTime.Parse(value.ToString()) != null)
                            {
                                finalData = value == null ? "" : value.ToString();
                            }
                            else
                                throw new InvalidDataException(value
                                    , String.Format("The value '{0}' provided for the '{1}' ColumnType is not valid", value, DataType.Datetime.ToString()));
                            break;
                        case DataType.Double:
                            if (value == null
                                || value.ToString() == ""
                                || Convert.ToDouble(value.ToString().Length <= 1) != null)
                            {
                                finalData = value == null ? "" : value.ToString();
                            }
                            else
                                throw new InvalidDataException(value
                                    , String.Format("The value '{0}' provided for the '{1}' ColumnType is not valid", value, DataType.Double.ToString()));
                            break;
                        case DataType.Decimal:
                            if (value == null
                                || value.ToString() == ""
                                || Convert.ToDecimal(value.ToString().Length <= 1) != null)
                            {
                                finalData = value == null ? "" : value.ToString();
                            }
                            else
                                throw new InvalidDataException(value
                                    , String.Format("The value '{0}' provided for the '{1}' ColumnType is not valid", value, DataType.Decimal.ToString()));
                            break;
                        case DataType.Integer:
                            if (value == null
                                || value.ToString() == ""
                                || Convert.ToInt32(value.ToString().Length <= 1) != null)
                            {
                                finalData = value == null ? "" : value.ToString();
                            }
                            else
                                throw new InvalidDataException(value
                                    , String.Format("The value '{0}' provided for the '{1}' ColumnType is not valid", value, DataType.Integer.ToString()));
                            break;
                        case DataType.String:
                            if (value == null
                                || value.ToString() == "")
                            {
                                finalData = value == null ? "" : value.ToString();
                                /*if (finalData != "")
                                {
                                    SharedStringData sharedString = worksheet.GetSharedString();
                                    if ((sharedStringIndex = sharedString.GetReader().CheckSharedString(finalData)) == -1)
                                        sharedStringIndex = sharedString.GetWriter().WriteSharedString(finalData);
                                }*/
                            }
                            else
                                throw new InvalidDataException(value
                                    , String.Format("The value '{0}' provided for the '{1}' ColumnType is not valid", value, DataType.String.ToString()));
                            break;
                    } 
                    #endregion

                    dataValue = finalData; //worksheet.GetWriter().Write(cellAddress.ToString(), finalData);
                }
                catch (Exception ex)
                {
                    throw new InvalidDataException(value, ex.Message);
                }
            }
        }

        public DataType Type
        {
            get
            {
                return Style.Type;
            }
        }

        public CellStyle Style
        {
            get
            {
                style = StyleLoader.GetInstance().CellStyles(styleIndex);
                return style;
            }
            set
            {
                style = value;
            }
        }

        #endregion
    }
}
